package com.lw.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.yygh.enums.DictEnum;
import com.lw.yygh.model.user.Patient;
import com.lw.yygh.user.feign.CmnFeignClient;
import com.lw.yygh.user.mapper.PatientMapper;
import com.lw.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private CmnFeignClient cmnFeignClient;
    @Override
    public List<Patient> findAllByUserId(Long userId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id",userId );
        List<Patient> list = baseMapper.selectList(wrapper);
        //通过远程调用对就诊人地址信息进行封装
        list.stream().forEach( item->{
            this.packPatient( item );
        });
        return list;
    }

    @Override
    public Patient getPatientById(Long id) {
        Patient patient = baseMapper.selectById( id );
        Patient packPatient = this.packPatient( patient );
        return packPatient;
    }

    private Patient packPatient(Patient patient) {
        //根据证件编号和value查询证件类型
        String certificatesTypeString = cmnFeignClient.getName( DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType() );
        //联系人证件类型
        String contactsCertificatesTypeString =
                cmnFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = cmnFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = cmnFeignClient.getName(patient.getCityCode());
        //区
        String districtString = cmnFeignClient.getName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
